<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>入库管理</title>
<!-- 导入jquery核心类库 -->
<%@include file="../include/jqueryeasyui.jsp"%>
<script type="text/javascript">
	function addfenchang(pcode) {
		var parent$ = self.parent.$; //找到父级DOM  

		var text = pcode + "分场";
		var url = "page.htfenchang.ashx?pcode=" + pcode;
		if (parent$("#tabs").tabs('exists', text)) {
			parent$('#tabs').tabs('select', text);
		} else {
			var content = '<div style="width:100%;height:100%;overflow:hidden;">'
					+ '<iframe src="'
					+ url
					+ '" scrolling="auto" style="width:100%;height:100%;border:0;" ></iframe></div>';

			parent$('#tabs').tabs('add', {
				title : text,
				content : content,
				closable : true
			});
		}
	}

	function addpinlei(pcode) {
		var parent$ = self.parent.$; //找到父级DOM  

		var text = pcode + "品类";
		var url = "page.pinlei.ashx?pcode=" + pcode;
		if (parent$("#tabs").tabs('exists', text)) {
			parent$('#tabs').tabs('select', text);
		} else {
			var content = '<div style="width:100%;height:100%;overflow:hidden;">'
					+ '<iframe src="'
					+ url
					+ '" scrolling="auto" style="width:100%;height:100%;border:0;" ></iframe></div>';

			parent$('#tabs').tabs('add', {
				title : text,
				content : content,
				closable : true
			});
		}
	}

	function configOrder(pcode) {
		$
				.post(
						"${pageContext.request.contextPath}/clothesType/page.findByTypeCPIDSerial.ashx?typeCPIDSerial="
								+ pcode,
						function(result) {
							if (result != null && result.length > 0) {
								var str = "";
								for (var i = 0; i < result.length; i++) {
									str += "<div><div style='width:300px;float:left'>"
											+ result[i].typeName
											+ "</div><input style='width:50px' id='"+result[i].typeID+"' type='text' value = '"+result[i].displayOrder+"'/></div>"
								}
								$('#configOrderWindow_center').html(str);
								$('#configOrderWindow').window('open');
							} else {
								$.messager
										.alert("提示", "该项目尚未配置品类，无需设置", "info");
							}
						});
	}

	function saveDisplayOrder() {
		var data = "";
		$("#configOrderWindow_center input[type='text']").each(function() {
			data += $(this).attr("id") + ":" + $(this).val() + ",";
		});
		data = data.substring(0, data.length - 1);
		if (data != "") {
			$
					.post(
							"${pageContext.request.contextPath}/clothesType/page.saveDisplayOrder.ashx?data="
									+ data, function(result) {
								if (result.status == "1") {
									$.messager.alert("提示", "设置成功", "info");
									$('#configOrderWindow').window('close');
								} else {
									$.messager.alert("提示", "设置失败，请重试", "info");
								}
							});
		} else {
			$.messager.alert("提示", "该项目尚未配置品类，无需设置", "info");
			$('#configOrderWindow').window('close');
		}
	}

	function choosepinlei(pcode) {
		$.messager.alert("提示", "未完待续，敬请期待！<br> 请先使用添加专属品类功能。", "info");
	}

	function doAdd() {

		$('#project_name_tr').show();
		$('#project_code_tr').show();
		$('#public_pinlei_tr').show();
		$('#remark_tr').show();

		$('#outputWindow').form('clear');
		$('#outputWindow *').trigger('blur');// 离焦
		$('#outputWindow').window("open").window('setTitle', '创建项目');
		$('#cPIDContract').val("-1");
	}

	function getQueryParams(queryParams) {
		var searchcontent = $('#searchcontent').val();
		queryParams.v_creatorName = searchcontent;

		var project_name = $('#project_name').val();
		queryParams.cPName = project_name;

		return queryParams;

	}

	//增加查询参数，重新加载表格  
	function reloadgrid() {

		//查询参数直接添加在queryParams中      
		var queryParams = $('#grid').datagrid('options').queryParams;
		getQueryParams(queryParams);
		$('#grid').datagrid('options').queryParams = queryParams;
		$("#grid").datagrid('load');
	}

	function doSearch() {
		reloadgrid();
	}

	function doEdit() {

		var rowData = $('#grid').datagrid('getSelected');
		if (rowData == null) {
			$.messager.alert("提示", "请选择要修改的行！", "info");
		} else {
			$('#project_name_tr').show();
			$('#project_code_tr').show();
			$('#public_pinlei_tr').hide();
			$("input[type='checkbox']").attr("checked", false);
			$('#remark_tr').hide();

			$("input[name='cPIDContract']").val(rowData.cPIDContract);
			$("input[name='cPIDSerial']").val(rowData.cPIDSerial);
			$("input[name='cPName']").val(rowData.cPName);
			$("input[name='oldCPIDSerial']").val(rowData.oldCPIDSerial);

			// 弹出窗口

			$("#outputWindow").window('open').window('setTitle', '修改项目');
		}
	}

	function doAddPinlei() {

		var rowData = $('#grid').datagrid('getSelected');
		if (rowData == null) {
			$.messager.alert("提示", "请选择要修改的行！", "info");
		} else {

			$("input[name='cPIDContract']").val(rowData.cPIDContract);
			$("input[name='cPIDSerial']").val(rowData.cPIDSerial);
			$("input[name='cPName']").val(rowData.cPName);

			$('#project_name_tr').hide();
			$('#project_code_tr').hide();
			$('#public_pinlei_tr').show();
			$('#remark_tr').show();

			$('#outputWindow').window("open").window('setTitle', '配置常用品类');
		}
	}

	// 定义列
	var columns = [ [ {
		field : 'cPIDSerial',
		title : '项目代码',
		width : 120,
	}, {
		field : 'cPName',
		title : '项目名称',
		width : 500,
	}, {
		field : 'createTime',
		title : '创建时间',
		width : 160,
		formatter : formatterDate
	}, {
		field : 'v_creatorName',
		title : '创建人',
		width : 160,
		formatter : formatterCreatorName
	}, {
		field : 'cPIDContract',
		title : '操作',
		width : 260,

		formatter : formatterId
	} ] ];

	function formatterId(value, row, index) {
		var cPIDSerial = row.cPIDSerial;
		return "<a href=# onclick=addfenchang(\'"
				+ cPIDSerial
				+ "\')>添加分场</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=configOrder(\'"
				+ cPIDSerial
				+ "\') href=#>配置品类顺序</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=doDel(\'"
				+ cPIDSerial + "\') href=#>删除</a>";
	}

	function doDel(pcode) {

		$.messager
				.confirm(
						'消息',
						"<p style='line-height:24px;'>确认要删除合同号为"
								+ pcode
								+ "的合同？<br><font color='red'>本操作将会把该合同下所有的分场信息、花名册信息、测量数据信息全部删除!</font></p>",
						function(r) {
							if (r) {
								$('#pid').val(pcode);
								$('#inputPwdWindow').window('open');
							}
						});

	}
	function formatterCreatorName(value, row, index) {

		if (value) {
			return value;
		} else {
			return "接口转入"
		}
	}

	function doProcess(index) {
		$('#grid').datagrid('selectRow', index);// 关键在这里  
		var row = $('#grid').datagrid('getSelected');
		if (row) {
			//alert(row.iRId)
			location.href = "${pageContext.request.contextPath}/page.inputrulerdetails.ashx?iRId="
					+ row.iRId;

		}
	}

	function formatterDate(value, row, index) {
		if (value == '' || null == value) {
			return '';
		}
		var now = new Date(value);
		var year = now.getYear() + 1900;
		var month = now.getMonth() + 1;
		var date = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		minute = (minute > 9) ? minute = minute : minute = '0' + minute;
		//var second=now.getSeconds(); 
		return year + "-" + month + "-" + date + " " + hour + ":" + minute;
	}

	function formateroBIsDone(value, row, index) {
		if (value == false) {
			return "否";
		} else {
			return "是";
		}
	}

	$(function() { //main
		$('#add').click(doAdd);
		$('#addpinlei').click(doAddPinlei);
		$('#edit').click(doEdit);
		$('#search').click(doSearch);
		$('#downloadxls').click(doDownload);
		initDeleteFunctionButtons();
		downloadPublicClothesType();
		initValidate();
	});

	function downloadPublicClothesType() {

		$
				.post(
						"${pageContext.request.contextPath}/clothesType/page.findByTypeCategory.ashx?typeCategory="
								+ '0',
						function(data) {
							if (data.length > 0) {

								for (var i = 0; i < data.length; i++) {
									$('#public_pinlei_td')
											.append(
													'<label>'
															+ '<input name="cPClothesTypes" type="checkbox" value='+data[i].typeID +'>'
															+ data[i].typeName
															+ '</label><br/><br/>');
								}

							}
						});
	}

	function doDownload() {

		location.href = "${pageContext.request.contextPath}/mingdan.xls";
	}
	$(function() {
		// 先将body隐藏，再显示，不会出现页面刷新效果
		$("body").css({
			visibility : "visible"
		});

		// 收派标准数据表格
		$('#grid').datagrid({
			iconCls : 'icon-forward',
			fit : true,
			border : false,
			singleSelect:true,
            pageSize: 10,
            pagination: true, //是否启用分页
            //rownumbers: true, //是否显示列数
			striped : true,
			toolbar : '#tb',
			url : "${pageContext.request.contextPath}/contract/page.list.ashx",
			idField : 'sId',
			columns : columns,
			pageList : [ 10, 20, 50, 100 ],
		 	view : myview,
			emptyMsg : "<font size=5>暂无记录</font>"			
		});

		//初始化员工窗口
		$('#outputWindow').window({
			title : '入库',
			width : 450,
			modal : true,
			shadow : true,
			closed : true,
			height : 500,
			resizable : false

		});

		//提交表单
		$('#save')
				.click(
						function() {

							/* 	if($("input[type='checkbox']:checked").length>1){
									$.messager.alert('消息','只能选择一个常用品类！','info');
									return;
								} */

							// 校验
							if ($('#sform').form('validate')) {
								// 1 、获得form 数据
								var params = $('#sform').serialize();
								// 2、将数据发送给服务器 
								$
										.post(
												"${pageContext.request.contextPath}/contract/page.saveOrUpdate.ashx",
												params,
												function(data) {
													if (data == "success") {
														// 成功
														$.messager
																.alert(
																		'消息',
																		'操作成功 ',
																		'info');
														// 关闭窗口
														$('#outputWindow')
																.window('close');
														// 清除表单数据
														$('#outputWindow')
																.form('clear');
														$('#outputWindow *')
																.trigger('blur');// 离焦

														// 刷新表格数据 
														$('#grid').datagrid(
																'reload');
													} else if (data == "conflict") {
														$.messager.alert('消息',
																'项目代码重复',
																'info');
													} else if (data == "nameconflict") {
														$.messager
																.alert(
																		'消息',
																		'品类名称重复，请到项目下修改品类名称后重新增加品类。',
																		'info');
													} else if (data == "publicclothesonlyone") {
														$.messager.alert('消息',
																'只能选择一个常用品类',
																'info');
													} else if (data == "clothestypeonlyone") {
														$.messager
																.alert(
																		'消息',
																		'每个项目下只准有一个品类。<br><br>请到“添加专属品类”下查看该项目品类。',
																		'info');
													} else {
														data = new String(data);
														if (data.charAt(0) == '<') {
															$.messager.alert(
																	'消息',
																	'没有权限 ',
																	'info');
														} else {
															$.messager.alert(
																	'消息',
																	'操作失败 ',
																	'info');
														}

													}

												});
							}

						});

	});

	var myview = $.extend({}, $.fn.datagrid.defaults.view, {
		onAfterRender:function(target){
			$.fn.datagrid.defaults.view.onAfterRender.call(this,target);
			var opts = $(target).datagrid('options');
			var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
			vc.children('div.datagrid-empty').remove();
			if (!$(target).datagrid('getRows').length){
				var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || 'no records').appendTo(vc);
				d.css({
					position:'absolute',
					left:0,
					top:50,
					width:'100%',
					textAlign:'center'
				});
			}
		}
	});

	function initValidate() {
		$.extend($.fn.validatebox.defaults.rules, {

			projectCode : {
				validator : function(value) {
					var reg = /^[0-9a-zA-Z]+$/;
					return reg.test(value);
				},
				message : '只能输入数字和字母'
			}
		})
	}
	function initDeleteFunctionButtons() {

		$("#btnCancel").click(function() {
			$('#inputPwdWindow').window('close');
		});

		$("#btnEp")
				.click(
						function() {
							var pwd = $("#inputpwd").val();
							var pid = $("#pid").val();
							$
									.ajax({
										url : '${pageContext.request.contextPath}/contract/page.delContractFamily.ashx?pwd='
												+ pwd + '&pid=' + pid,
										type : 'POST',

										dataType : 'json',
										success : function(data) {
											if (data.status == 1) {
												$.messager.alert("提示", "删除成功",
														"info");
												$('#inputPwdWindow').window(
														'close');
												reloadgrid();
											} else {
												$.messager.alert("提示", "删除失败",
														"info");
											}
										}
									});
						});

	}
</script>
</head>
<body class="easyui-layout">
	<div region="center">

		<table id="grid">

		</table>
	</div>

	<div class="easyui-window" id="outputWindow" collapsible="false"
		minimizable="false" maximizable="false" style="top: 50px; left: 200px">
		<div region="north" style="height: 31px; overflow: hidden;"
			split="false" border="false">
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton"
					plain="true">保存</a>
			</div>
		</div>
		<div region="center" style="overflow: auto; padding: 5px;"
			border="false">
			<form id="sform">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">项目详情</td>
					</tr>
					<input type="hidden" name="cPIDContract" id="cPIDContract" value="" />
					<tr id="project_name_tr">
						<td>项目名称</td>
						<td><input name="cPName" id="cPName" type="text"
							class="easyui-validatebox" required="true" /></td>
					</tr>

					<tr id="project_code_tr">
						<td>项目代码</td>
						<td><input name="cPIDSerial" id="cPIDSerial" type="text"
							class="easyui-validatebox" required="true"
							validtype="projectCode" /><input name="oldCPIDSerial"
							id="oldCPIDSerial" type="hidden" value="" /></td>
					</tr>

					<tr id="public_pinlei_tr">
						<td>配置常用品类</td>
						<td id="public_pinlei_td"></td>
					</tr>

					<tr id="remark_tr">
						<td colspan="2">*选择常用品类后，将自动复制一份常用品类到所选项目下作为专属品类。</td>
					</tr>
					<tr>
						<td colspan="2">*“添加专属品类”功能可以对所选项目下所有品类进行再次编辑。</td>
					</tr>
				</table>
			</form>
		</div>
	</div>



	<div id="tb" style="padding: 5px; height: auto">
		<div style="margin-bottom: 5px">
			<a href="#" id="add" class="easyui-linkbutton" iconCls="icon-add"
				plain="true">增加</a> <a href="#" id="edit" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true">编辑</a> <a href="#" id="addpinlei"
				class="easyui-linkbutton" iconCls="icon-edit" plain="true">配置常用品类</a>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;项目名称: <input
				id="project_name" class="easyui-textbox" style="width: 80px">
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;创建人: <input
				id="searchcontent" class="easyui-textbox" style="width: 80px">
			<a href="#" id="search" class="easyui-linkbutton"
				iconCls="icon-search">搜索</a>
		</div>
	</div>


	<!--输入密码窗口-->
	<div id="inputPwdWindow" class="easyui-window" title="请输入登录密码授权"
		collapsible="false" minimizable="false" modal="true" closed="true"
		resizable="false" maximizable="false" icon="icon-save"
		style="width: 400px; height: 160px; padding: 5px; background: #fafafa">
		<div class="easyui-layout" fit="true">
			<div region="center" border="false"
				style="padding: 10px; background: #fff; border: 1px solid #ccc;">
				<table cellpadding=3>

					<tr>
						<td>您的登陆密码：</td>
						<td><input id="inputpwd" type="password" class="txt01" /></td>

					</tr>
					<tr>

						<td><input type="hidden" id="pid" type="text" /></td>

					</tr>
				</table>
			</div>
			<div region="south" border="false"
				style="text-align: right; height: 30px; line-height: 30px;">
				<a id="btnEp" class="easyui-linkbutton" icon="icon-ok"
					href="javascript:void(0)">确定</a> <a id="btnCancel"
					class="easyui-linkbutton" icon="icon-cancel"
					href="javascript:void(0)">取消</a>
			</div>
		</div>
	</div>
	<div id="configOrderWindow" class="easyui-window" title="配置app端品类显示顺序"
		collapsible="false" minimizable="false" modal="true" closed="true"
		resizable="false" maximizable="false" icon="icon-save"
		style="width: 400px; height: 300px; padding: 5px; background: #fafafa">
		<div region="north" style="height: 31px; overflow: hidden;"
			split="false" border="false">
			<div class="datagrid-toolbar">
				<a id="saveorder" onclick='saveDisplayOrder()' icon="icon-save"
					href="#" class="easyui-linkbutton" plain="true">保存</a>
			</div>
		</div>
		<div region="center" id="configOrderWindow_center"
			style="overflow: auto; padding: 5px;" border="false"></div>
	</div>
</body>
</html>